package 力扣.双指针;

public class 环形链表141 {
    public boolean hasCycle(ListNode head) {
        ListNode fast = null;
        ListNode slow = null;
        if (head != null){
            fast = head.next;
            slow = head;
        }else {
            return false;
        }
         while (fast != null){
             if (fast.next != null){
                 fast = fast.next.next;
                 slow = slow.next;
             }else {
                 return false;
             }
             if (fast == slow){
                 return true;
             }
         }
         return false;
    }
    //利用假头不必判断head节点是否为空
    public boolean hasCycle2(ListNode head) {
        ListNode dummy = new ListNode();
        dummy.next = head;
        ListNode fast = dummy;
        ListNode slow = dummy;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow){
                return true;
            }
        }
        return false;
    }
}
